<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import cache from '@/utils/cache'
import useLoginStore from '@/store/login/login'
import { rules } from '../config/account-config'

const formRef = ref<FormInstance>()

const account = reactive({
  name: cache.getCache('name') ?? '',
  password: cache.getCache('password') ?? ''
})

const loginStore = useLoginStore()

const loginAction = (isKeepPassword: boolean) => {
  formRef.value?.validate((valid) => {
    if (valid) {
      // 1. 判断是否需要记住密码
      if (isKeepPassword) {
        // 本地缓存
        cache.setCache('name', account.name)
        cache.setCache('password', account.password)
      } else {
        cache.deleteCache('name')
        cache.deleteCache('password')
      }
      // 2. 开始进行登录验证
      /*
        1. 登录的逻辑（网络请求，拿到数据后的处理）
        2. 数据保存到某一个位置
        3. 发送其他的请求（请求当前用户的信息）
        4. 拿到用户的菜单
        5. 跳转到首页
      */
      loginStore.accountLoginAction({ ...account })
    }
  })
}
defineExpose({ loginAction }) // 把方法暴露出去，不然无法通过 comRef.func 的方式访问
</script>

<template>
  <div class="login-account">
    <el-form ref="formRef" :model="account" :rules="rules">
      <el-form-item label="账号" prop="name" :label-width="60">
        <el-input v-model="account.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :label-width="60">
        <el-input v-model="account.password" type="password"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
// .login-account {
// }
</style>
